package hash.medium;

import java.util.HashMap;
import java.util.Map;

/**
 * TinyURL 的加密与解密
 * TinyURL是一种URL简化服务，
 * 比如：当你输入一个URL
 * https://leetcode.com/problems/design-tinyurl时，
 * 它将返回一个简化的URL http://tinyurl.com/4e9iAk.
 *
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/encode-and-decode-tinyurl
 */
public class EncodeAndDecodeTinyURL_535 {
    Map<Integer, String> map = new HashMap<>();

    // Encodes a URL to a shortened URL.
    public String encode(String longUrl) {
        int i = longUrl.hashCode();
        map.put(i, longUrl);
        return "http://tinyurl.com/" + i;
    }

    // Decodes a shortened URL to its original URL.
    public String decode(String shortUrl) {
        return map.get(Integer.parseInt(shortUrl.replace("http://tinyurl.com/", "")));
    }
}
